package cn.leizp.springboot.server.netmanger.snmp;

import org.snmp4j.*;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SnmpUtilV1 {

    private static final int PORT = 161;
    private static final int TIMEOUT = 3000;
    private static final int RETRIES = 3;

    /**
     * 执行 SNMPv1 GET 请求
     * 
     * @param ip        设备 IP 地址
     * @param oid       OID
     * @param community 社区名
     * @return 返回值
     */
    public static String snmpGetV1(String ip, String oid, String community) {
        try {
            // 创建 TransportMapping 并监听
            TransportMapping<UdpAddress> transport = new DefaultUdpTransportMapping();
            transport.listen();

            // 创建 SNMP 实例
            Snmp snmp = new Snmp(transport);

            // 创建目标对象
            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString(community));
            target.setAddress(new UdpAddress(ip + "/" + PORT));
            target.setRetries(RETRIES);
            target.setTimeout(TIMEOUT);
            target.setVersion(SnmpConstants.version1);

            // 创建 PDU
            PDU pdu = new PDU();
            pdu.add(new VariableBinding(new OID(oid)));
            pdu.setType(PDU.GET);

            // 发送请求
            ResponseEvent event = snmp.send(pdu, target);
            if (event != null) {
                if (event.getError() != null) {
                    System.out.println(oid + "响应:" + event.getError().getMessage());
                }
                PDU response = event.getResponse();
                if (response != null) {
                    VariableBinding vb = response.getVariableBindings().get(0);
                    if (vb != null) {
                        return vb.getVariable().toString();
                    }
                }
            }

            // 关闭连接
            snmp.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 遍历 OID 获取绑定信息
     * 
     * @param ip        设备 IP 地址
     * @param oidPrefix OID 前缀
     * @param community 社区名
     * @return 绑定信息列表
     */
    public static List<String> getIpMacBindingInfo(String ip, String oidPrefix, String community) {
        List<String> bindingInfo = new ArrayList<>();
        try {
            TransportMapping<UdpAddress> transport = new DefaultUdpTransportMapping();
            transport.listen();
            Snmp snmp = new Snmp(transport);

            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString(community));
            target.setAddress(new UdpAddress(ip + "/" + PORT));
            target.setRetries(RETRIES);
            target.setTimeout(TIMEOUT);
            target.setVersion(SnmpConstants.version1);

            PDU pdu = new PDU();
            pdu.add(new VariableBinding(new OID(oidPrefix)));
            pdu.setType(PDU.GETNEXT);

            ResponseEvent event;
            do {
                event = snmp.send(pdu, target);
                if (event != null) {
                    PDU response = event.getResponse();
                    if (response != null && response.getErrorStatus() == PDU.noError) {
                        VariableBinding vb = response.getVariableBindings().get(0);
                        if (vb.getOid().startsWith(new OID(oidPrefix))) {
                            bindingInfo.add(vb.getOid() + " = " + vb.getVariable());
                            pdu.setVariableBindings(response.getVariableBindings());
                        } else {
                            break;
                        }
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            } while (true);

            snmp.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bindingInfo;
    }

    public static void main(String[] args) throws IOException {
        String ip = "192.168.55.1";
        String community = "publicCommunity";
        String community2 = "1qaz2wsx";
        // String oid = SwitchOIDs.IF_NUMBER;
        String oid = "1.3.6.1.2.1.4.22.1.2";
        String value = "my huawei";

        // 执行 SNMPv1 GET 请求
        String getResult = snmpGetV1(ip, oid, community);
        if (getResult != null) {
            System.out.println(oid + " GET 响应: " + getResult);
        }

        // 华为设备示例 OID 前缀
        //oid = "1.3.6.1.2.1.17.4.3.1.1";
        // oid = "1.3.6.1.4.1.2011.5.25.123.1.17.1.11";
        getResult = snmpGetV1(ip, oid, community);
        if (getResult != null) {
            System.out.println(oid + " GET 响应: " + getResult);
        }

        List<String> macTableInfo = getIpMacBindingInfo(ip, oid, community2);
        if (!macTableInfo.isEmpty()) {
            System.out.println("华为交换机 MAC 地址表内容如下：");
            for (String info : macTableInfo) {
                System.out.println(info);
            }
        } else {
            System.out.println("未获取到华为交换机 MAC 地址表内容。");
        }

        // 执行 SNMPv1 SET 请求
        // boolean setResult = snmpSetV1(ip, oid, value, community);
        // System.out.println(oid + " SET 结果: " + setResult);
    }
}